
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client implements Runnable, Observer
{
	private BufferedReader reader;
	private PrintWriter writer;
	private Socket socket;
	private UserList userList;
	public Client(Socket socket, UserList userList) throws IOException
	{
		this.socket = socket;
		this.userList = userList;
		reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		writer = new PrintWriter(socket.getOutputStream());
	}
	public void run()
	{
		String input;
		try 
		{
			while ((input=reader.readLine())!=null)
			{
				userList.updateUsers(input);
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				reader.close();
				socket.close();
				userList.removeUser(this);
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}
	@Override
	public void update(String message) 
	{
		writer.println(message);
		writer.flush();
	}
}

